package buke;

import java.util.*;

public class Board {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int[][] board = new int[n][m];
		
		for(int i = 0; i<n; i++) {
			for(int j = 0; j<m; j++) {
				board[i][j] = sc.nextInt();
			}
		}
   boolean flg = true;
    	if(checkWon(board)) {
    	flg= true;
    	}else {
    		flg = false;
    }
		
	System.out.println(flg);
		
	}
 
	public static boolean checkWon(int[][] board) {
        // write code here[[1,0,1],[1,-1,-1],[1,-1,0]]
       	int N=board.length;
		int i=0;
		int j = 0;
		int sum = 0;
		//判断行,如果一行元素的和为3,说明当前玩家胜出,返回true
		for(i=0;i<N;i++) {
			sum =0;
			for(j = 0;j<N;j++) {
				sum+=board[i][j];
			}
			if(sum==N) {
				return true;
			}
		}
		//列
		for(j = 0;j<N;i++) {
			sum=0;
			for(i = 0;i<N;i++) {
				sum+=board[i][j];
			}
			if(sum==N) {
				return true;
			}
		}
		sum=0;
		//主对角线
		for(i=0;i<N;i++) {
			sum+=board[i][i];
		}
		if(sum==N) {
			return true;
		}
	sum= 0;
	//副对角线
	for(i=0;i<N;i++) {
		sum+=board[i][N-1-i];
	}
	if(sum==N) {
		return true;
	}
	
	return false;
	}
    }